package com.service.impl;

import com.constant.Constant;
import com.dao.ChapterDao;
import com.dao.ResourceDao;
import com.entity.Chapter;
import com.entity.Resource;
import com.entity.User;
import com.exception.DataAccessException;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.entity.Chapter;
import com.service.ChapterService;
import com.util.AjaxResult;
import com.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

import java.util.List;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
//===张
@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    ChapterDao chapterDao;
    @Autowired
    ResourceDao resourceDao;
    @Override
    public PageInfo<Chapter> loadChapters(Integer pageNo, Chapter chapter, @DateTimeFormat(pattern="yyyy-MM-dd") Date begin_date, @DateTimeFormat(pattern="yyyy-MM-dd") Date end_date){
        PageHelper.startPage(pageNo, 4);
        System.out.println("loadChapters================");
        List<Chapter> chapters = chapterDao.findAll(chapter,begin_date,end_date);
        System.out.println("loadChapters1111111111111111");
        PageInfo<Chapter> info = new PageInfo<>(chapters);
        System.out.println("loadChapters222222222222222222221");
        return info;
    }
    //添加章节
    @Override
    public AjaxResult insertChapter(Chapter chapter) {
        if(null != chapterDao.selectChapterByTitle(chapter.getTitle())){
            return AjaxResult.isFail("标题已经存在");
        }
        int i=chapterDao.insertChapter(chapter);
        if(i==1){
            return AjaxResult.isOk("添加数据成功");
        }

        return  AjaxResult.isFail("添加失败");
    }

    @Override
    //修改章节前的查询
    public Chapter findChapterById(Integer id){
        System.out.println("findChapterById===");
        Chapter chapter = chapterDao.selectChapterById(id);
        System.out.println(chapter+"1111111111111");
//        chapter.obj
        return chapter;
    }


    @Override
    //修改章节信息
    public AjaxResult updateChapter(String title,String info,Integer id){
        int i = chapterDao.updateChapterByID(title,info,id);
        System.out.println(i+"2222");
        if(i==1){
            return AjaxResult.isOk("修改成功");
        }
        return AjaxResult.isFail("修改失败");
    }


    @Override
    //修改状态
    public boolean updateChapterStatuss(Integer id, Integer status){
        int i = chapterDao.updateStatusById(id, status);
        return i>0 ? true:false;
    }



    //===张
    @Override
    public List<Chapter> findChaptersByCourseId(Integer courseId) {
        List<Chapter> chapters = chapterDao.selectChaptersByCourseId(courseId);
        for (Chapter chapter : chapters) {
            Integer chapterId = chapter.getId();
            //根据chapter的Id查找对应的资源Set<Resource>
            HashSet<Resource> resources = resourceDao.selectResourceSetByChapterId(chapterId);
            //setResources(Set<Resource> resources) 将根据chapter的id查出来的资源放到chapter的对象中
            chapter.setResources(resources);

        }
        return chapters;
    }




}

